package com.flexdms.i18n;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Alternative;
import javax.inject.Inject;

import org.apache.deltaspike.core.api.config.ConfigProperty;

import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.translate.Translate;
import com.google.api.services.translate.TranslateRequestInitializer;
import com.google.api.services.translate.model.TranslationsListResponse;
import com.google.api.services.translate.model.TranslationsResource;

@ApplicationScoped
@Alternative
public class GoogleTranslator extends BaseTranslator
{

	@Inject
	@ConfigProperty(name="com.flexdms.i18n.googletranslator.key")
	String key;
	
	@Inject
	@ConfigProperty(name="com.flexdms.i18n.googletranslator.userip")
	String userip;
	
	@Inject
	@ConfigProperty(name="com.flexdms.i18n.googletranslator.appname")
	String appname;
	
	
	public void setKey(String key)
	{
		this.key = key;
	}
	Translate translate;
	@PostConstruct
	public void init()
	{

		
		HttpTransport httpTransport = new NetHttpTransport();
		JsonFactory jsonFactory = new JacksonFactory();
		TranslateRequestInitializer initializer=new TranslateRequestInitializer(key);
		if (userip!=null)
		{
			initializer=new TranslateRequestInitializer(key, userip);
		}
		Translate.Builder builder = new Translate.Builder(httpTransport, jsonFactory,
				null)
				.setTranslateRequestInitializer(initializer);
		if (appname!=null)
		{
			builder.setApplicationName(appname);
		}
		translate=builder.build();
		
	}
	
	@Override
	protected List<String> _translate(List<String> qStrings, Locale locale) throws IOException
	{
		
		List<String> resultStrings=new ArrayList<String>(qStrings.size());
		
		int offset=0;
		while(offset<qStrings.size())
		{
			 offset=_translate(qStrings, locale, resultStrings, offset);
		}
		return resultStrings;

	}
	
	protected int _translate(List<String> qStrings, Locale locale, List<String> results, int offset) throws IOException
	{
		int count=0;
		int i=offset;
		for(; i<qStrings.size() && count<1800; i++)
		{
			count+=qStrings.get(i).length();
		}
		
		//check this
		Translate.Translations.List service = translate.translations().list(qStrings.subList(offset, i), localeToLanguage(locale));
		service.setSource(localeToLanguage(sourceLocale));
		TranslationsListResponse response = service.execute();

		for(TranslationsResource result:response.getTranslations())
		{
			results.add(result.getTranslatedText());
		}
		return i;

	}
	
	protected String localeToLanguage(Locale locale)
	{
		String lang=locale.getLanguage().toLowerCase();
		if (lang.equalsIgnoreCase("zh"))
		{
			if (locale.getCountry().length()>0)
			{
				lang="zh-"+locale.getCountry().toLowerCase();
			} 
			
		}
		return lang;
	}
	
}
